
layui.use('form', function(){
    var form = layui.form,authorityId=getQueryVariable("authorityId"),flag=getQueryVariable("flag"),type=getQueryVariable("type");

    getMenuByCode(authorityId,flag,type);

    /*if(type=="M"){//修改
        $("#code").val(authorityId);
        getMenuByCode(authorityId,flag,type);
    }else{//新增

        if(flag=="T"){//新增同级菜单
            getMenuByCode(authorityId,flag,type);
        }
        if(flag=="X"){//新增下级菜单

        }
    }*/

    //监听提交
    form.on('submit(menu-form-element)', function(data){

        if(flag=="M"){
            $.ajax({
                type : 'POST',
                data:JSON.stringify(data.field),
                url:'menu/update',
                dataType:"json",
                contentType: 'application/json',
                async:false,
                success : function (da) {
                    if(da.status==200 && da.data.length >0){
                        // datas=da.data

                        // datas= initData(da.data[0].list);
                        layer.msg("修改成功!");
                    }
                },
                error: function(data) {
                    layer.msg("修改失败!");
                }
            });
        }else{

           /* $.ajax({
                type : 'POST',
                //  url : 'resource/getTree',
                data:{"code":data.field.code},
                url:'menu/getExeitBycode',
                dataType:"json",
               /!* contentType: 'application/json',*!/
                async:false,
                success : function (da) {
                    if(da.status==200 ){
                      if(da.data){
                          layer.mss("编码重复！");

                      }else{

                      }


                    }
                },
                error: function(data) {
                    //layer.msg("、!");
                }
            });*/
            $.ajax({
                type : 'POST',
                data:JSON.stringify(data.field),
                url:'menu/insert',
                dataType:"json",
                contentType: 'application/json',
                async:false,
                success : function (da) {
                    if(da.status==200 && da.data.length >0){
                        // datas=da.data

                        // datas= initData(da.data[0].list);
                        layer.msg("新增成功!");
                    }
                },
                error: function(data) {
                    layer.msg("新增失败!");
                }
            });

        }

       // parent.layui.admin.events.closeThisTabs();//关闭当前tab页
        //parent.detab("views/jsp/SystemCenter/menusettings.jsp");
       /* layer.msg(JSON.stringify(data.field));
        //关闭窗口
        // parent.detab("views/jsp/SystemCenter/tpl/newupmenu.jsp"+(authorityId==''?'':'?authorityId='+authorityId));
        return false;*/
        setTimeout(function () {
            //保存成功则刷新提交过来的界面 并关闭此页面
            parent.detab("views/jsp/SystemCenter/menusettings.jsp");
            parent.intab("views/jsp/SystemCenter/menusettings.jsp","菜单设置");
           if(type=="M"){
               parent.detab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type+"&&id="+id);
           }
           if(flag=="T"){
               parent.detab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type);
           }
            if(flag=="X"){
                parent.detab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type);
            }
            if(flag=="C"){
                parent.detab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type);
            }
        },1000);
        return false;
      /*  if(type=="M"){
            parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type+"&&id="+id,"修改菜单");
        }
        if(flag=="T"){
            parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type,"新增同级菜单");
        }
        if(flag=="X"){
            parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type,"新增下级菜单");
        }
        if(flag=="C"){
            parent.intab("views/jsp/SystemCenter/tpl/newupmenu.jsp?authorityId="+authorityId+"&&flag="+flag+"&&type="+type,"新增菜单");
        }*/
    });
    //根据code（传递过来的参数）获取当臆回显的信息
    function getMenuByCode(code,flag,type) {
        $.ajax({
            type : 'POST',
            //  url : 'resource/getTree',
            url:'menu/getMenuByCode',
            data:{"code":code,"flag":flag,"type":type},
            dataType:"json",

            async:false,
            success : function (da) {
                if(da.status==200 ){

                    $("#name").val(da.data.name);
                    $("#parentName").val(da.data.parentName);
                    $("#pid").val(da.data.pid);
                    $("#id").val(da.data.id);
                }
            },
            error: function(data) {
                layer.msg("初始化失败!");
            }
        });
        if(type=="M"){
            $("#code").val(code);
            $("#code")[0].disabled=true;
        }else{
            $("#name").val("");
            $("#code").val("");
            $("#code")[0].disabled=false;
           // $("#parentName").val(da.data.parentName);
          //  $("#pid").val(da.data.pid);
        }
        if(flag=="M"){
            $("span")[0].innerHTML="修改菜单";
           // $("#newTitle").val("修改菜单");
        }
        if(flag=="T"){
            $("span")[0].innerHTML="新增同级菜单";
           // $("#newTitle").val("新增同级菜单");
        }
        if(flag=="X"){
            $("span")[0].innerHTML="新增下级菜单";
            //$("#newTitle").val("新增下级菜单");
        }
        if(flag=="C"){
            $("span")[0].innerHTML="新增菜单";
          //  $("#newTitle").val("新增菜单");
            $("#level").val("1");
        }else{
            $("#level").val("0");
        }

        $("#parentName")[0].disabled=true;
    }
});
